'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var React = _interopRequireWildcard(_react);

var _pcadata = require('../pcadata');

var _pcadata2 = _interopRequireDefault(_pcadata);

var _uxcore = require('uxcore');

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

var data = _pcadata2['default']; /**
                                  * 省市区选择组件(多选)
                                  * @author: ljq
                                  * @since: 2019-04-17 16:17:07
                                  */

var FILL_CHAR = '00';
if (localStorage.pcadata) {
    data = JSON.parse(localStorage.pcadata);
}
if (data['86'] && Object.keys(data['86'])[0].length === 2) {
    FILL_CHAR = '';
}

var PcaMultiSelect = function (_React$Component) {
    (0, _inherits3['default'])(PcaMultiSelect, _React$Component);

    function PcaMultiSelect() {
        (0, _classCallCheck3['default'])(this, PcaMultiSelect);

        var _this = (0, _possibleConstructorReturn3['default'])(this, (PcaMultiSelect.__proto__ || Object.getPrototypeOf(PcaMultiSelect)).apply(this, arguments));

        _this.getProvince = function () {
            var provinces = data['86'];
            var options = [];
            for (var provinceCode in provinces) {
                if (!provinces.hasOwnProperty(provinceCode)) {
                    continue;
                }
                options.push({
                    value: provinceCode,
                    label: provinces[provinceCode]
                });
            }
            return options;
        };
        _this.getCity = function () {
            var provinces = data['86'];
            var options = [];
            for (var provinceCode in provinces) {
                if (!provinces.hasOwnProperty(provinceCode)) {
                    continue;
                }
                var p = {
                    value: provinceCode,
                    label: provinces[provinceCode],
                    children: []
                };
                var citys = data[provinceCode];
                for (var cityCode in citys) {
                    if (!citys.hasOwnProperty(cityCode)) {
                        continue;
                    }
                    p.children.push({
                        value: cityCode,
                        label: citys[cityCode]
                    });
                }
                options.push(p);
            }
            return options;
        };
        _this.getArea = function () {
            var provinces = data['86'];
            var options = [];
            for (var provinceCode in provinces) {
                if (!provinces.hasOwnProperty(provinceCode)) {
                    continue;
                }
                var p = {
                    value: provinceCode,
                    label: provinces[provinceCode],
                    children: []
                };
                var citys = data[provinceCode];
                for (var cityCode in citys) {
                    if (!citys.hasOwnProperty(cityCode)) {
                        continue;
                    }
                    var c = {
                        value: cityCode,
                        label: citys[cityCode],
                        children: []
                    };
                    var areas = data[cityCode];
                    for (var areaCode in areas) {
                        if (!areas.hasOwnProperty(areaCode)) {
                            continue;
                        }
                        c.children.push({
                            value: areaCode,
                            label: areas[areaCode]
                        });
                    }
                    p.children.push(c);
                }
                options.push(p);
            }
            return options;
        };
        _this.getOptions = function () {
            var level = _this.props.level;

            if (level === '1') {
                return _this.getProvince();
            }
            if (level === '2') {
                return _this.getCity();
            }
            if (level === '3') {
                return _this.getArea();
            }
            return [];
        };
        _this.renderText = function () {
            var value = _this.props.value;

            if (!value) return '';
            var names = value.map(function (code) {
                // 省
                if (code.substr(2, 6) === FILL_CHAR + FILL_CHAR) {
                    return data['86'][code] || '';
                }
                // 市
                if (code.substr(4, 6) === FILL_CHAR) {
                    var cityProvinceCode = code.substr(0, 2) + FILL_CHAR + FILL_CHAR;
                    var citys = data[cityProvinceCode];
                    return citys && citys[code] || '';
                }
                // 区
                var areaCityCode = code.substr(0, 4) + FILL_CHAR;
                var areas = data[areaCityCode];
                if (!areas) {
                    var areaProvinceCode = code.substr(0, 2) + FILL_CHAR + FILL_CHAR;
                    var _citys = data[areaProvinceCode];
                    return _citys && _citys[code] || '';
                }
                return areas && areas[code] || '';
            });
            return names.join('、');
        };
        _this.renderCombinationText = function () {
            var value = _this.props.value;

            if (!value) return '';
            //合并
            var codeList = value;
            value.forEach(function (code) {
                // 省
                if (code.substr(2, 6) === FILL_CHAR + FILL_CHAR) {
                    return;
                }
                // 市
                if (code.substr(4, 6) === FILL_CHAR) {
                    var cityProvinceCode = code.substr(0, 2) + FILL_CHAR + FILL_CHAR;
                    var citys = data[cityProvinceCode] && Object.keys(data[cityProvinceCode]) || [];
                    var sameProvinceCityCodeList = value.filter(function (i) {
                        return i.substr(2, 6) !== FILL_CHAR + FILL_CHAR && i.substr(4, 6) === FILL_CHAR && i.substr(0, 2) + FILL_CHAR + FILL_CHAR === cityProvinceCode;
                    }) || [];
                    if (sameProvinceCityCodeList.length !== citys.length) {
                        return;
                    }
                    if (JSON.stringify(sameProvinceCityCodeList.sort()) !== JSON.stringify(citys.sort())) {
                        return;
                    }
                    value = value.filter(function (i) {
                        return !(i.substr(2, 6) !== FILL_CHAR + FILL_CHAR && i.substr(4, 6) === FILL_CHAR && i.substr(0, 2) + FILL_CHAR + FILL_CHAR === cityProvinceCode);
                    }) || [];
                    value.push(cityProvinceCode);
                    return;
                }
                // 区
                var areaCityCode = code.substr(0, 4) + FILL_CHAR;
                var areas = data[areaCityCode] && Object.keys(data[areaCityCode]) || [];
                var sameCityAreaCodeList = value.filter(function (i) {
                    return i.substr(2, 6) !== FILL_CHAR + FILL_CHAR && i.substr(4, 6) !== FILL_CHAR && i.substr(0, 4) + FILL_CHAR === areaCityCode;
                }) || [];
                if (sameCityAreaCodeList.length !== areas.length) {
                    return;
                }
                if (JSON.stringify(sameCityAreaCodeList.sort()) !== JSON.stringify(areas.sort())) {
                    return;
                }
                value = value.filter(function (i) {
                    return !(i.substr(2, 6) !== FILL_CHAR + FILL_CHAR && i.substr(4, 6) !== FILL_CHAR && i.substr(0, 4) + FILL_CHAR === areaCityCode);
                }) || [];
                value.push(areaCityCode);
                return;
            });
            var names = value.map(function (code) {
                // 省
                if (code.substr(2, 6) === FILL_CHAR + FILL_CHAR) {
                    return data['86'][code] || '';
                }
                // 市
                if (code.substr(4, 6) === FILL_CHAR) {
                    var cityProvinceCode = code.substr(0, 2) + FILL_CHAR + FILL_CHAR;
                    var citys = data[cityProvinceCode];
                    return citys && citys[code] || '';
                }
                // 区
                var areaCityCode = code.substr(0, 4) + FILL_CHAR;
                var areas = data[areaCityCode];
                if (!areas) {
                    var areaProvinceCode = code.substr(0, 2) + FILL_CHAR + FILL_CHAR;
                    var _citys2 = data[areaProvinceCode];
                    return _citys2 && _citys2[code] || '';
                }
                return areas && areas[code] || '';
            });
            return names.join('、');
        };
        return _this;
    }

    (0, _createClass3['default'])(PcaMultiSelect, [{
        key: 'render',
        value: function render() {
            var _props = this.props,
                type = _props.type,
                model = _props.model;

            if (type === 'text') {
                return React.createElement(
                    'span',
                    this.props,
                    this.renderText()
                );
            }
            if (type === 'combination') {
                return React.createElement(
                    'span',
                    this.props,
                    this.renderCombinationText()
                );
            }
            if (model) {
                return React.createElement(_uxcore.CascadeMultiSelect.CascadeMultiModal, (0, _extends3['default'])({ placeholder: '\u8BF7\u9009\u62E9\u5730\u533A' }, this.props, { cascadeSize: Number(this.props.level), options: this.getOptions() }));
            }
            return React.createElement(_uxcore.CascadeMultiSelect, (0, _extends3['default'])({ placeholder: '\u8BF7\u9009\u62E9\u5730\u533A' }, this.props, { cascadeSize: Number(this.props.level), options: this.getOptions() }));
        }
    }]);
    return PcaMultiSelect;
}(React.Component);

exports['default'] = PcaMultiSelect;
module.exports = exports['default'];